//
//  BIZLiveHistoryAnswerVC.m
//  biz
//
//  Created by LXL on 2017/6/20.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZLiveHistoryAnswerVC.h"
#import "BIZLiveHistoryAnswerModel.h"
#import "BIZLiveHistoryAnswerCell.h"

static NSString *kCellID = @"BIZLiveHistoryAnswerCell";

@interface BIZLiveHistoryAnswerVC ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *liveAnswerArr;
@property (assign, nonatomic) NSInteger lastId;

@end

@implementation BIZLiveHistoryAnswerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"往期问答";
    
    [self configTableView];
    
    [self loadLiveHistoryAnswerData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.liveAnswerArr.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BIZLiveHistoryAnswerCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    cell.liveHistoryAnswerModel = self.liveAnswerArr[indexPath.row];
    
    return cell;
}

- (void)configTableView {
    self.tableView.estimatedRowHeight = 300;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.lastId = 1;
        [self.liveAnswerArr removeAllObjects];
        [self loadLiveHistoryAnswerData];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadLiveHistoryAnswerData];
    }];
    
}

- (void)loadLiveHistoryAnswerData {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"pageIndex"] = @(self.lastId);
    dic[@"pageEnd"] = @(self.lastId+9);
    
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:kLiveHistoryAnswerListUrl parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DeLog(@"往期回答：%@", responseObject);
        [MBProgressHUD hideHUD];
        [self stopRefresh];
        
        BIZLiveHistoryAnswerListModel *listModel = [BIZLiveHistoryAnswerListModel yy_modelWithJSON:responseObject];
        if (listModel.code == 1) {
            if (!listModel.data.count) {
                self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
            } else {
                [self.liveAnswerArr addObjectsFromArray:listModel.data];
                self.lastId +=listModel.data.count;
                [self.tableView reloadData];
            }
            
            if (self.liveAnswerArr.count) {
                BIZLiveHistoryAnswerModel *model = [self.liveAnswerArr firstObject];
                if (model.atime) {
                    [BIZFileManager saveUserData:@(model.atime) forKey:@"atime"];
                }
                
            }
            
        } else {
            if (listModel.code == 2) {
                BIZLoginViewController *vc = [BIZLoginViewController viewControllerFromStoryboard];
                
                MJWeakSelf
                vc.loginSuccessBlock = ^(){
                    weakSelf.lastId = 1;
                    [weakSelf.liveAnswerArr removeAllObjects];
                    [weakSelf loadLiveHistoryAnswerData];
                };
                
                [self presentViewController:vc animated:YES completion:nil];
            
                
            } else {
                [MBProgressHUD showErrorMessage:listModel.des];

            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DeLog(@"往期回答：%@", error);
        [MBProgressHUD hideHUD];
        [self stopRefresh];
        
    }];
    
}

- (void)stopRefresh {
    if (self.tableView.mj_header.isRefreshing || self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }

}

- (NSInteger)lastId {
    if (!_lastId) {
        _lastId = 1;
    }
    
    return _lastId;
}


- (NSMutableArray *)liveAnswerArr {
    if (!_liveAnswerArr) {
        _liveAnswerArr = [NSMutableArray array];
    }
    
    return _liveAnswerArr;
}

@end
